package com.example.huadu_server.landlord.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.huadu_server.landlord.entity.LandlordSchedule;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

@Mapper
public interface LandlordScheduleMapper extends BaseMapper<LandlordSchedule> {

    @Select("SELECT * FROM landlord_schedule " +
            "WHERE landlord_id = #{landlordId} " +
            "AND schedule_date BETWEEN #{startDate} AND #{endDate} " +
            "ORDER BY schedule_date, start_time")
    List<LandlordSchedule> selectSchedulesByLandlordIdAndDateRange(@Param("landlordId") int landlordId,
                                                                   @Param("startDate") Date startDate,
                                                                   @Param("endDate") Date endDate);


    /**
     * 根据房东ID查询未来档期（分页）
     *
     * @param landlordId
     * @param status
     * @return
     */
    @Select("SELECT * FROM landlord_schedule " +
            "WHERE landlord_id = #{landlordId} " +
            "AND schedule_date >= CURDATE() " + // 只查询未来的档期
            "AND (#{status} IS NULL OR status = #{status}) " + // 根据状态过滤
            "ORDER BY schedule_date, start_time ")
    // 分页
    List<LandlordSchedule> selectFutureSchedulesByLandlordId(@Param("landlordId") Integer landlordId,
                                                             @Param("status") String status);

    /**
     * 根据房东ID查询所有档期（分页）
     *
     * @param landlordId
     * @param status
     * @return
     */
    @Select("SELECT * FROM landlord_schedule " +
            "WHERE landlord_id = #{landlordId} " +
            "AND (#{status} IS NULL OR status = #{status}) " + // 根据状态过滤
            "ORDER BY schedule_date, start_time ")
    List<LandlordSchedule> selectAllSchedulesByLandlordId(@Param("landlordId") Integer landlordId,
                                                          @Param("status") String status);


    /**
     * 根据房东ID查询档期数量
     *
     * @param landlordId
     * @param status
     * @param isFuture
     * @return
     */
    @Select("<script>" +
            "SELECT COUNT(*) FROM landlord_schedule " +
            "WHERE landlord_id = #{landlordId} " +
            "<if test='status != null'> AND status = #{status} </if>" + // 根据状态过滤
            "<if test='isFuture'> AND schedule_date >= CURDATE() </if>" + // 是否查询未来档期
            "</script>")
    int countSchedulesByLandlordId(@Param("landlordId") Integer landlordId,
                                   @Param("status") String status,
                                   @Param("isFuture") boolean isFuture);
}
